package com.tianyu.jty.system.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jswzc.api.service.smsUnified.SmsUnifiedServiceI;
import com.jswzc.api.service.wxTemplate.WxTemplateServiceI;
import com.tianyu.jty.system.entity.ScheduleJob;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务工作类
 * @author ty
 * @date 2015年1月13日
 */
@DisallowConcurrentExecution
@Component("TaskA")
public class TaskA implements Job {

    private SmsUnifiedServiceI smsUnifiedService;
    @Reference
    private WxTemplateServiceI wxTemplateService;

    public void execute(JobExecutionContext context) throws JobExecutionException {
        ClassPathXmlApplicationContext contexts = new ClassPathXmlApplicationContext(
                "classpath*:/applicationContext-dubbo.xml");
        contexts.start();
        smsUnifiedService = (SmsUnifiedServiceI) contexts.getBean("smsUnifiedService"); // 获取远程服务代理
        String[] phones={"18860858018","18505142701","15252572334","15050746353"};
        if (smsUnifiedService!=null){
            Map<String, Object> res = new HashMap<String, Object>();
            try {
                for(String phone:phones){
                    res=smsUnifiedService.sendSms(phone, "123456为您的验证码", "SMS-1001","KAAA234NMS242");
                }
            } catch (Exception e) {
                res.put("res", "F");
                res.put("msg", "未知错误:" + e.getMessage());
            }
            System.out.print(8888888);
        }else{
            System.out.println(22222);
        }
        ScheduleJob scheduleJob = (ScheduleJob)context.getMergedJobDataMap().get("scheduleJob");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");    
	    System.out.println("任务名称 = [" + scheduleJob.getName() + "]"+ " 在 " + dateFormat.format(new Date())+" 时运行"); 
    }
}
